package by.ad.fusiontables;

import static com.google.common.base.Charsets.UTF_8;
import com.google.gdata.client.GoogleService;
import com.google.gdata.client.Service.GDataRequest;
import com.google.gdata.client.Service.GDataRequest.RequestType;
import com.google.gdata.util.ContentType;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;

public class SelectEngine
{
  public static final String SERVICE_URL =
    "https://www.google.com/fusiontables/api/query";
  
  public static InputStream runSelect(final String selectQuery) 
    throws IOException, ServiceException 
  {
    final URL url = new URL(
      SERVICE_URL + "?sql=" + URLEncoder.encode(selectQuery, UTF_8.name()));
    
    final GoogleService service = new GoogleService(
      "fusiontables", "fusiontables.by.ad");
    final GDataRequest request = service.getRequestFactory().getRequest(
      RequestType.QUERY, url, ContentType.TEXT_PLAIN);
    
    request.execute();
    return request.getResponseStream();
  }
  
}
